/**
 * 
 */
package com.wolfpire.chapter.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.chapter.dao.SubjectChapterRelationDao;
import com.wolfpire.knowledge.dao.SubjectKnowledgeRelationDao;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.SubjectChapterRelation;
import com.wolfpire.system.model.SubjectKnowledgeRelation;

/**
 * @author lihd
 *
 */

@Repository("subjectChapterRelationDao")
public class SubjectChapterRelationDaoImpl extends BaseHibernateDao<SubjectChapterRelation, Long> implements SubjectChapterRelationDao {

	public void relate(Long subjectId, Long chapterId) {
		//先删除后绑定
		this.deleteByChapterId(chapterId);
		if (null != subjectId) {
			SubjectChapterRelation sc = new SubjectChapterRelation(chapterId,subjectId);
			this.saveOrUpdate(sc);
		}
	}
	
	private void deleteByChapterId(Long chapterId) {
		String executeSql = "DELETE FROM t_subject_chapter WHERE chapter_id = ?";
		this.executeSql(executeSql, chapterId);
			
	}

	public SubjectChapterRelation getSCRelation(Long chapterId) {
		Criteria c = createCriteria(Restrictions.eq("chapterId", chapterId));
		Object obj = c.uniqueResult();
		if (null != obj) {
			return (SubjectChapterRelation) obj;
		}
		return null;
	}

	public List<SubjectChapterRelation> getCSRelations(Long subjectId) {
		Criteria c = createCriteria(Restrictions.eq("subjectId", subjectId), Restrictions.isNotNull("chapterId"));
		return c.list();
	}
	

}
